﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using MvcEngine.Core.Repositories;

using System.Web;
using MvcEngine.Core;
using MvcEngine.Mvc.Models.ViewModels;
using MvcEngine.Core.Content;
using MvcEngine.Core.Helpers;

namespace MvcEngine.Mvc.Filters
{
    public class ModuleActionFilter : IActionFilter
    {
        public void OnActionExecuted(ActionExecutedContext filterContext)
        {

        }

        public void OnActionExecuting(ActionExecutingContext filterContext)
        {
            Control control = null;

            if (filterContext.RouteData.Values.ContainsKey("controlKey"))
            {
                control = ContentManager.Current.GetControlByKey(filterContext.RouteData.Values["controlKey"] as string);
            }

            if (control != null)
            {
                Module module = ContentManager.Current.GetModule(control.ModuleId);
                filterContext.RouteData.Values["moduleName"] = module.Name;
                filterContext.HttpContext.Items["moduleName"] = module.Name;

                filterContext.RouteData.Values["controlId"] = control.Id;
                filterContext.ActionParameters["controlId"] = control.Id;
            }
        }
    }
}
